﻿using System;
using System.Data;
using System.Linq;
namespace WPFExcelReport
{
    public partial class ViewModelBase
    {
         private bool _IsPage = false; public bool IsPage { get { return _IsPage; } set { _IsPage = value; OnPropertyChanged("IsPage", value); } }
        private int _Pages = 0; public int Pages { get { return _Pages; } set { _Pages = value; OnPropertyChanged("Pages", value); } }
        private int _PageNow = 0; public int PageNow { get { return _PageNow; } set { _PageNow = value; OnPropertyChanged("PageNow", value); } }
        private int _Counts = 0;
        public int Counts
        {
            get { return _Counts; }
            set { _Counts = value; OnPropertyChanged("Counts", value); }
        }
       
        private int _Counts2 = 0;
        public int Counts2
        {
            get { return _Counts; }
            set { _Counts2 = value; OnPropertyChanged("Counts2", value); }
        }
        private int _Rows = 0; public int Rows { get { return _Rows; } set { _Rows = value; OnPropertyChanged("Rows", value); } }
        private bool _FTAUTOSUM = false; public bool FTAUTOSUM { get { return _FTAUTOSUM; } set { _FTAUTOSUM = value; OnPropertyChanged("FTAUTOSUM", value); } }
        private bool _FTRESET = true; public bool FTRESET { get { return _FTRESET; } set { _FTRESET = value; OnPropertyChanged("FTRESET", value); } }

         private double _SUM1 = 0; public double SUM1 { get { return _SUM1; } set { _SUM1 = value; OnPropertyChanged("SUM1", value); } }
         private int _Counts1 = 0; public int Counts1 { get { return _Counts; } set { _Counts1 = value; OnPropertyChanged("Counts1", value); } }
         private string _KEY1 = ""; public string KEY1 { get { return _KEY1; } set { _KEY1 = value; OnPropertyChanged("KEY1", value); } }
         private string _SKEY1 = ""; public string SKEY1 { get { return _SKEY1; } set { _SKEY1 = value; OnPropertyChanged("SKEY1", value); } }
         private string _TCOUNT1 = ""; public string TCOUNT1 { get { return _TCOUNT1; } set { _TCOUNT1 = value; OnPropertyChanged("TCOUNT1", value); } }
         private string _TKEY1 = ""; public string TKEY1 { get { return _TKEY1; } set { _TKEY1 = value; OnPropertyChanged("TKEY1", value); } }
    
        private string _TKEY2 = ""; public string TKEY2 { get { return _TKEY2; } set { _TKEY2 = value; OnPropertyChanged("TKEY2", value); } }
        private string _TCOUNT2 = ""; public string TCOUNT2 { get { return _TCOUNT2; } set { _TCOUNT2 = value; OnPropertyChanged("TCOUNT2", value); } }
        private string _KEY2 = ""; public string KEY2 { get { return _KEY2; } set { _KEY2 = value; OnPropertyChanged("KEY2", value); } }
        private string _SKEY2 = ""; public string SKEY2 { get { return _SKEY2; } set { _SKEY2 = value; OnPropertyChanged("SKEY2", value); } }
        private double _SUM2 = 0;
        public double SUM2
        {
            get { return _SUM2; }
            set { _SUM2 = value; OnPropertyChanged("SUM2", value); }
        }

        private double _SUM3 = 0; public double SUM3 { get { return _SUM3; } set { _SUM3 = value; OnPropertyChanged("SUM3", value); } }
        private int _Counts3 = 0; public int Counts3 { get { return _Counts; } set { _Counts3 = value; OnPropertyChanged("Counts3", value); } }
        private string _KEY3 = ""; public string KEY3 { get { return _KEY3; } set { _KEY3 = value; OnPropertyChanged("KEY3", value); } }
        private string _SKEY3 = ""; public string SKEY3 { get { return _SKEY3; } set { _SKEY3 = value; OnPropertyChanged("SKEY3", value); } }
        private string _TCOUNT3 = ""; public string TCOUNT3 { get { return _TCOUNT3; } set { _TCOUNT3 = value; OnPropertyChanged("TCOUNT3", value); } }
        private string _TKEY3 = ""; public string TKEY3 { get { return _TKEY3; } set { _TKEY3 = value; OnPropertyChanged("TKEY3", value); } }

        public DataSet Set;
        public DelegateCommand PrePageCommand { get; set; }
        public DelegateCommand NextPageCommand { get; set; }
        int rest = 0;
        public void NextPage()
        { 
            if (Set == null)
                return;
            if (PageNow == Pages)
            {
                TBMain.Merge(Set.Tables[0]);
                return;
            }  
            rest = Counts - PageNow * Rows;
            if (rest > Rows)
            {
                rest = Rows;
            }
            if (rest <= 0)
                return;
            var result = from pdt in Set.Tables[0].AsEnumerable()
                         select pdt;
            DataTable tb = result.Skip(PageNow * Rows).Take(rest).CopyToDataTable(); 
            TBMain.Merge(tb);
            PageNow++;
        }

        public void PrePage()
        {
            if (Set == null)
                return;
            if (PageNow == 1)
                return;
            PageNow--;
            DataTable tb; 
            var result = from pdt in Set.Tables[0].AsEnumerable()
                         select pdt;
            rest = Rows;
            tb = result.Skip((PageNow - 1) * Rows).Take(rest).CopyToDataTable();
            TBMain.Clear();
            TBMain.Merge(tb);

        }

        public virtual void MergeData(DataSet _Set, bool IsMove = true, bool IsClear = true)
        {

            bool ftsum= FTAUTOSUM;
            bool ftreset = FTRESET;
            FTAUTOSUM = false;
            FTRESET = false;
            try
            {
                SUM1 = 0;
                Counts1 = 0;
                SUM2 = 0; 
                Counts2 = 0;
                SUM3 = 0;
                Counts3 = 0;
                Set = _Set;
                if (IsClear)
                {
                    TBMain.Clear();
                }
                PageNow = 0;
                Counts = Set.Tables[0].Rows.Count; 
                if (!IsPage)
                {
                    TBMain.Merge(Set.Tables[0]); 
                    return;
                } 
                Pages = System.Convert.ToInt16(Math.Ceiling((decimal)Counts / Rows));
                NextPage();               
            }
            catch (Exception ex)
            {

            }
            finally
            {
                FTAUTOSUM = ftsum;
                FTRESET = ftreset;
                ReSetTBMain();
            }
        }

        public void ReSetTBMain()
        {
            try
            {
                SUM1 = 0;
                Counts1 = 0;
                SUM2 = 0;
                Counts2 = 0; 
                SUM3 = 0;
                Counts3 = 0;
               
                if (!FTAUTOSUM)
                    return;
                if (!FTRESET)
                    return;
                double YQTY = 0, YMIN = 0.0, YMAX = 0.0, YMID = 0.0;
                int YNS = 0;
               
                DETECTTB(SKEY1, KEY1, ref YQTY, ref YMIN, ref YMIN, ref YMID, ref YNS);
                SUM1 = YQTY;
                Counts1 = YNS;
               
                DETECTTB(SKEY2, KEY2, ref YQTY, ref YMIN, ref YMIN, ref YMID, ref YNS);
                SUM2 = YQTY;
                Counts2 = YNS;
               
                DETECTTB(SKEY3, KEY3, ref YQTY, ref YMIN, ref YMIN, ref YMID, ref YNS);
                SUM3 = YQTY;
                Counts3 = YNS;
            }
            catch (Exception ex)
            {
 
            }
        }
        private void DETECTTB(string X, string Y, ref double YQTY, ref double YMIN, ref double YMAX, ref double YMID, ref int YNS)
        {
            if (X.Trim().Length == 0)
                return;
            if (!TBMain.Columns.Contains(X)) return;
            if (!TBMain.Columns.Contains(Y)) return;
            var qData = from XD in TBMain.AsEnumerable()
                        where XD.RowState != DataRowState.Deleted
                        group XD by new
                        {
                            KEY = XD[X].ToString(),
                        }
                            into g
                            select new
                            {
                                KEY = g.Key,
                                YMAX = g.Max(XD => Convert.ToDouble(XD[Y].ToString())),
                                YMIN = g.Min(XD => Convert.ToDouble(XD[Y].ToString())),
                                YMID = g.Average(XD => Convert.ToDouble(XD[Y].ToString())),
                                YQTY = g.Sum(XD => Convert.ToDouble(XD[Y].ToString())),
                                YNS = g.Count(),
                            };
            foreach (var XD in qData)
            {
                string key = (string)XD.KEY.KEY;
                YQTY = XD.YQTY;
                YMAX = XD.YMAX;
                YMIN = XD.YMIN;
                YMID = XD.YMID;
                YNS = XD.YNS;
            }
        }
    }

}
